package com.keon.jianbao.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.keon.jianbao.AppConfig;
import com.keon.jianbao.AppContext;
import com.keon.jianbao.AppManager;
import com.keon.jianbao.R;
import com.keon.jianbao.api.Api;
import com.keon.jianbao.api.SimpleHandler;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.base.BaseFragment;
import com.keon.jianbao.base.BaseWebViewClient;
import com.keon.jianbao.bean.Expert;
import com.keon.jianbao.bean.Result;
import com.keon.jianbao.enu.WebType;
import com.keon.jianbao.ui.activity.UploadJianBaoActivity;
import com.keon.jianbao.util.JsonUtils;
import com.keon.jianbao.util.StringUtils;

import org.apache.http.Header;
import org.json.JSONException;

import butterknife.Bind;

/**
 * Created by keon on 15/9/17.
 */
public class WebFragment extends BaseFragment {

    int type = 0;

    private CookieManager cookie;

    private String mCurrentUrl;

    @Bind(R.id.webview)
    WebView wvWeb;

    SimpleHandler expertDetailHandler = new SimpleHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            super.onSuccess(statusCode, headers, responseBody);
            try {
                Result result = new Result(responseBody);
                if (result.getStatus() > 0) {
                    BaseActivity ba = (BaseActivity) AppManager.getAppManager().currentActivity();
                    Bundle bundle = new Bundle();
                    Expert expert = JsonUtils.fromJson(result.getContent(), Expert.class);
                    bundle.putSerializable("expert", expert);
                    ba.jump(UploadJianBaoActivity.class, bundle);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_web;
    }

    @Override
    public void initView(View view) {

        cookie = CookieManager.getInstance();
        WebSettings webSettings = wvWeb.getSettings();
        webSettings.setJavaScriptEnabled(true); // 启用支持javascript
        wvWeb.setSaveEnabled(false);
        //设置Web视图
        wvWeb.setWebViewClient(new MyWebViewClient());

        wvWeb.addJavascriptInterface(this, "to_appraisal_call");
    }

    @Override
    public void initData() {
        expertDetailHandler.setContext(getActivity());

        Bundle bundle = getArguments();
        type = bundle.getInt("type");
        if (type != 0) {

            WebType wt = WebType.getWebType(type);
            String url = wt.getUrl();
            if (type == 3) {
                int expert_id = bundle.getInt("expert_id");
                url += expert_id;
            } else if (type == 4) {
                int activity_id = bundle.getInt("activity_id");
                url += activity_id;
            }

            onUrlLoading(wvWeb, url);
        } else {
            String url = bundle.getString("url");
            if (!StringUtils.isEmpty(url)) {
                onUrlLoading(wvWeb, url);
            }
        }
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 载入链接之前会被调用
     *
     * @param view WebView
     * @param url  链接地址
     */
    protected void onUrlLoading(WebView view, String url) {
//        mProgress.setVisibility(View.VISIBLE);
        cookie.setCookie(url,
                AppContext.getInstance().getProperty(AppConfig.CONF_COOKIE));
        cookie.setCookie(url,
                "PHPSESSID=" + AppContext.getInstance().getProperty("user.sessionid"));
        view.loadUrl(url);
    }

    /**
     * 链接载入成功后会被调用
     *
     * @param view WebView
     * @param url  链接地址
     */
    protected void onUrlFinished(WebView view, String url) {
        mCurrentUrl = url;
//        mProgress.setVisibility(View.GONE);
    }

    private class MyWebViewClient extends BaseWebViewClient {

    }

    @JavascriptInterface
    public void clickOnAndroid(String expert_id) {

        Api.expert_detail(Integer.parseInt(expert_id), expertDetailHandler);
    }
}
